package com.djhu.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.constant.RequestRes;
import com.entity.Dep;

@RestController
public class ConsumerController {
	
	private static final Logger log = LoggerFactory.getLogger(ConsumerController.class);
	
//	public static final String QUERY_URL = "http://localhost:8001"; // 直接调用IP + 端口
	public static final String QUERY_URL = "http://SPRINGCLOUD-PROVIDER"; // 调用微服务方法
	
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value="/list",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	public boolean queryAll() {
		// url : Rest 请求地址; request: 请求参数； responseType: Http响应转换和被转换的对象类型
		log.info("ConsumerController queryAll start !!!!");
		if(restTemplate.getForObject(QUERY_URL + "/list", Dep.class) != null){
			return true;
		}
		return false;
	}
	
	@RequestMapping(value="/descover",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	public RequestRes descoverByProvider() {
		// url : Rest 请求地址; request: 请求参数； responseType: Http响应转换和被转换的对象类型
		log.info("ConsumerController descoverByProvider start !!!!");
		RequestRes requestRes = restTemplate.getForObject(QUERY_URL + "/discover", RequestRes.class);
		for (int i = 0; i < 1000; i++) {
			if(requestRes==null){
				throw new Error();
			}
		}
		if(requestRes != null){
			return requestRes;
		}
		return RequestRes.ReError();
	}
	

}
